package com.thinkingstar.iads.projectcenter.serviceImpl;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.thinkingstar.iads.common.utils.InitSystemProperties;
import com.thinkingstar.iads.common.utils.POIFileUtil;
import com.thinkingstar.iads.common.utils.PictureUtil;
import com.thinkingstar.iads.projectcenter.entity.PcProjectFile;
import com.thinkingstar.iads.projectcenter.entity.dao.DaoPcProjectFile;

@IocBean
public class PreviewFileServiceImpl {
	
	@Inject
	private DaoPcProjectFile daoPcProjectFile;
	
	private static final String serverIp= InitSystemProperties.get("preview.server.ip");
	
	private static final String serverPort = InitSystemProperties.get("preview.server.port");
	
	private static final String previewServer = "http://"+serverIp+":"+serverPort;
	
	
	//http://121.60.84.103:8012/picturesPreview?currentUrl=/iadsFile/business/equipment/jpg/1534386058048.jpg&urls=/iadsFile/business/equipment/jpg/1534386058048.jpg
	//获取ifarm显示参数(多图片)
	public String getUrls(String [] file_ids){
		String frist_image = "";//显示的第一张图
		String all_images = "";
		for(int i=0;i<file_ids.length;i++){
			PcProjectFile pcProjectFile = daoPcProjectFile.fetch(file_ids[i]);
			if(pcProjectFile != null){
				String relative_path = pcProjectFile.getRelativePaths();
				String ab_path = POIFileUtil.getFileURIPathByRepath(relative_path);
				if(i == 0){
					frist_image = ab_path;
				}
				if(i == file_ids.length-1){
					all_images += ab_path;
				}else{
					all_images += ab_path + "%7C";
				}
			}
		}
		String url = previewServer+"/picturesPreview?currentUrl="+frist_image+"&urls="+all_images;
		return url;
	}
	
	//获取ifarm显示参数
	public String getUrl(String file_id , HttpServletRequest request){
		String serverIp= InitSystemProperties.get("preview.server.ip");
		String serverPort = InitSystemProperties.get("preview.server.port");
		String previewServer ="http://"+serverIp+":"+serverPort;
		PcProjectFile pcProjectFile = daoPcProjectFile.fetch(file_id);
		if(pcProjectFile != null){
			String fileExt = pcProjectFile.getFileExt();
			String relative_path = pcProjectFile.getRelativePaths();
			String ab_path = POIFileUtil.getFileURIPathByRepath(relative_path);
			String open_url = previewServer+ab_path;
			if(!fileExt.toUpperCase().equals("DWG")){
				String url = previewServer+"/onlinePreview?url="+open_url;
				return url;
			}else{
				String url = request.getContextPath()+"/projectCenter/previewCad/home/"+file_id;
				return url;
			}
		}
		return "";
	}
	
	//获取ifarm显示参数(多文件)
	public String getUrlByFiles(String [] file_ids){
		if(file_ids != null && file_ids.length > 0){
			List <File> file_list = new ArrayList<>();
			for(int i=0;i<file_ids.length;i++){
				PcProjectFile pcProjectFile = daoPcProjectFile.fetch(file_ids[i]);
				if(pcProjectFile != null){
					String re_path = pcProjectFile.getRelativePaths();
					String ab_path = POIFileUtil.getAbFileNameByRepath(re_path);
					File file = new File(ab_path);
					file_list.add(file);
				}
			}
			String re_path = POIFileUtil.getFileName(PictureUtil.createFileName("zip"), "zip", "zip");
			String ab_path = POIFileUtil.getAbFileNameByRepath(re_path);
			//文档预览路径
			Boolean bool = POIFileUtil.writeZip(file_list,ab_path);//将文件打包下载
			if(bool){
				String show_path = POIFileUtil.getFileURIPathByRepath(re_path);
				String open_url = previewServer+show_path;
				String url = previewServer+"/onlinePreview?url="+open_url;
				return url;
			}
		}
		return "";
	}
	
	
	//获取ifarm显示参数(单文件)
	public String getUrlById(String file_id){
		PcProjectFile pcProjectFile = daoPcProjectFile.fetch(file_id);
		if(pcProjectFile != null){
			String relative_path = pcProjectFile.getRelativePaths();
			String ab_path = POIFileUtil.getFileURIPathByRepath(relative_path);
			String open_url = previewServer+ab_path;
			String url = previewServer+"/onlinePreview?url="+open_url;
			return url;
		}
		return "";
	}

}
